(function(){
	angular.module('app')
		.controller('ctrl.point',['$scope','$uibModalInstance','config','http','myUtils','param',pointCtrl])
		.service('myPoint',['$rootScope','$uibModal','http', 'myUtils', pageCtrl])
		function pointCtrl($scope,$uibModalInstance,config,http,myUtils,param){
			var vm = $scope.vm = {};
			angular.extend(vm,{
				init : function(){
					vm.errorMessage = config.errorMessage;
					if( param ){
						vm.param = param || {};
					}
					vm.getInfo();
					vm.style_list= [];
				},
				getInfo : function(){
					/***获取投放点信息***/
					var url = config.getUrl('client_point');
					if( vm.param.id ){
						url = url.replace('{id?}',vm.param.id);
					}else{
						url = url.replace('{id?}','' );
					}
					if( vm.param.infoList && vm.param.infoList[0] ){
						vm.pointList = vm.param.infoList;
						return false;
					}
					http( url )
					.then( function( data ){
						if( data && ( data.error_code || data.error_message)){
							console.warn( data.error_message || config.error_tip );
							return false
						}
						if( data.data && data.data[0] && data.data[0][0]){
							var list =  data.data[0];
							var newList = [],oldId,idList = [];
							$.map( list,function( vv ){
								var itemList = {
									client_id : vv.client_id,
									client_name : vv.client_name,
									point :[]
								};
								$.map( list,function( item ){
									if( vv.client_id == item.client_id ){
										 item.launchpoint.stat = parseInt(item.stat);
										 item.launchpoint.state = item.stat ? true : false;
										 itemList.point.push( item.launchpoint);
									}
								});
								if( idList.indexOf( vv.client_id) < 0 ){
									idList.push(vv.client_id);
									newList.push(itemList);
								}
							});
							vm.pointList = newList;
							angular.forEach(data.data[0],function(v){ //已经选中的投放点样式
								if(v.stat == 1){
									var item = [];
									item.client_id = v.client_id;
									item.client_name = v.client_name;
									item.point_id = v.launchpoint.point_id;
									item.point_title = v.launchpoint.point_title;
									if(v.launchpoint.css_style){
										angular.forEach(v.launchpoint.css_style,function(vi){
											if(vi.stamp == 1){
												item.css_name_id = vi.css_name_id;
												item.css_name_content = vi.css_name_content;
											}
										})
									}
									//else {
									//	item.css_name_id = '';
									//	item.css_name_content = '';
									//}
									vm.style_list.push(item);
								}
							});
						}
					}) 
				},

				addStyle: function(item,point,style){
					var i = -1;
					angular.forEach(vm.style_list,function(vv){
						if(item.client_id == vv.client_id && point.point_id == vv.point_id ){
                        	i = 1;
							if(style){
								vv.css_name_id = style.css_name_id;
								vv.css_name_content = style.css_name_content;
							}
							return false;
						}
					});
					if(i == -1){
						var ii = [];
						ii.client_id = item.client_id;
						ii.client_name = item.client_name;
						ii.point_id = point.point_id;
						ii.point_title = point.point_title;
						if(style){
							ii.css_name_id = style.css_name_id;
							ii.css_name_content = style.css_name_content;
						}
						vm.style_list.push(ii);
					}
				},
				removeStyle : function(css){
					var idx = vm.style_list.indexOf(css);
					if (idx > -1) {
						vm.style_list.splice(idx, 1);
					}
				},
				save : function(){
					var saveData = [];
					angular.forEach(vm.style_list,function(vv){
						if(vv.css_name_content){
							param = vv.client_id +'-'+vv.client_name+','+vv.point_id +'-'+vv.point_title +','+vv.css_name_id+'-'+vv.css_name_content;
						}else{
							param = vv.client_id +'-'+vv.client_name+','+vv.point_id +'-'+vv.point_title;
						}

						saveData.push(param);
					});
					var url = config.getUrl('content_point');
					http(url,{content_id : vm.param.id,point_info:saveData},'post')
						.then(function(data){
							if(data.error_code == 0){
								myUtils.tip({
									type:'info',
									msg : '修改成功!'
								});
							}else{
								myUtils.tip({
									msg : data.error_message || config.error_tip
								});
								return false;
							}
						});
					$uibModalInstance.dismiss('cancel');
				},
				cancel : function(){
					$uibModalInstance.dismiss('cancel');
				}
			});
			vm.init();
		};
		function pageCtrl($rootScope,$uibModal,http, myUtils){
			return{
				showPoint  : function( cbk,param  ){
					 var modalInstance = $uibModal.open({
				          templateUrl: "../views/mxu/common/tpl/launchPoint.html",
				          controller: 'ctrl.point',
				          resolve: {
				            param: function () {
				              return param ;
				            }
				          }
			         });
			         modalInstance.result.then(function ( list ) {
			         	$.isFunction( cbk ) && cbk( list );
			        })
			   }
		  }
		}
})();